﻿using System;
using System.Collections.Generic;
using System.Text;
using Young3.FMSearch.Business.Managers;
using Young3.FMSearch.Business.Offsets;
using System.Linq;

namespace Young3.FMSearch.Business.Entities.InGame
{
    public class Team : BaseObject
    {
        public Team(int memoryAddress)
            : base(memoryAddress)
        { }

        public int ClubAddress
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + TeamOffsets.ClubAddress); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + TeamOffsets.ClubAddress); }
        }

        public Club Club
        {
            get
            {
                try
                {
                    return new Club(ClubAddress);
                }
                catch { return null; }
            }
            set { ClubAddress = value.MemoryAddress; }
        }

        public int NumberOfPlayers
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + TeamOffsets.NumberOfPlayers); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + TeamOffsets.NumberOfPlayers); }
        }

        private int StadiumAddress
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + TeamOffsets.StadiumAddress); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + TeamOffsets.StadiumAddress); }
        }

        private int LeagueAddress
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + TeamOffsets.League); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + TeamOffsets.League); }
        }

        public League League
        {
            get { return new League(LeagueAddress); }
            set { ProcessManager.WriteInt32(value.MemoryAddress, LeagueAddress); }
        }

        public int ID
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + TeamOffsets.Id); }
        }

        public Stadium Stadium
        {
            get { return new Stadium(StadiumAddress); }
            set { ProcessManager.WriteInt32(value.MemoryAddress, StadiumAddress); }
        }

        public TeamType Type
        {
            get { 
                switch ((ProcessManager.ReadByte(MemoryAddress + TeamOffsets.Type) + ProcessManager.ReadByte(MemoryAddress + TeamOffsets.Type + 0x3)))
                {
                    case 100: return TeamType.First;
                    case 256: return TeamType.Empty;
                    case 255: return TeamType.Amateur;
                    case 18: return TeamType.U18s;
                    case 19: return TeamType.U19s;
                    case 20: return TeamType.U19s;
                    case 21: return TeamType.U20s;
                    case 22: return TeamType.U21s;
                    case 23: return TeamType.B;
                    case 24: return TeamType.U23s;
                    case 101: return TeamType.Reserves;
                    default: return TeamType.Empty;
                }
            }
        }

        public int Prestige
        {
            get { return ProcessManager.ReadInt16(MemoryAddress + TeamOffsets.Prestige); }
            set { ProcessManager.WriteInt16(value, MemoryAddress + TeamOffsets.Prestige); }
        }
    }

    public enum TeamType
    {
        First, 
        Empty, 
        Amateur,
        U18s,
        U19s,
        U20s,
        U21s,
        B,
        U23s,
        Reserves
    }
}
